﻿using black_screen_mode_server.Models;
using DocumentFormat.OpenXml.EMMA;
using DocumentFormat.OpenXml.Office2010.Excel;
using DocumentFormat.OpenXml.Spreadsheet;
using MySqlX.XDevAPI;
using MySqlX.XDevAPI.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace black_screen_mode_server.DataCommunication
{
  //懒汉式单例模式创建DataCommunication类
  public class DataCommunicationMng
    {
        private static DataCommunicationMng instance = null;
  
        private Dictionary<Socket, int> clientInfos;
        private Dictionary<DataPacketType, Action<DataPacketType, string, Socket>> DpProcessesContainer;
        private Dictionary<int, CurrentGroupScreenInfos> groupScreenInfos;
        //读写锁
        private ReaderWriterLockSlim rwLock_ClientInfos = new ReaderWriterLockSlim();
        private ReaderWriterLockSlim rwLock_GroupScreenInfos= new ReaderWriterLockSlim();
        private ReaderWriterLockSlim rwLock_DpProcesesContainer = new ReaderWriterLockSlim();
        private ReaderWriterLockSlim rwLock_ServerRunFlag = new ReaderWriterLockSlim();
        private bool serverRunFlag = false;

        
        

        private DataCommunicationMng()
        {
            clientInfos = new Dictionary<Socket, int>();
            DpProcessesContainer = new Dictionary<DataPacketType, Action<DataPacketType, string, Socket>>();
            groupScreenInfos = new Dictionary<int, CurrentGroupScreenInfos>();
        }
        public static DataCommunicationMng GetInstance()
        {
            if (instance == null)
            {
                instance = new DataCommunicationMng();
            }
            return instance;
        }
   
        public bool GetServerRunFlag() 
        {
            bool flag = false;
            rwLock_ServerRunFlag.EnterReadLock();
            flag = serverRunFlag;
            rwLock_ServerRunFlag.ExitReadLock();
            return flag; 
        }

        public bool SetServerRunFlag(bool flag)
        {
            bool ret = false;
            rwLock_ServerRunFlag.EnterWriteLock();
            if (serverRunFlag != flag)
            {
                serverRunFlag = flag;
                ret = true;
            }
            rwLock_ServerRunFlag.ExitWriteLock();
            return ret;
        }
        public void RegisterClient(int clientId, Socket client)
        {
            bool initNotify = false;
            rwLock_ClientInfos.EnterWriteLock();
            try
            {
                //先删除clientInfos中含有相同clientId的client
                //Socket srcClient = null;
                //foreach (var item in clientInfos)
                //{
                //    if (item.Value == clientId)
                //    {
                //        srcClient = item.Key;
                //        break;
                //    }
                //}
                //if(srcClient != null)
                //    clientInfos.Remove(srcClient);
                if(!clientInfos.ContainsKey(client))
                {
                    clientInfos.Add(client, clientId);
                    initNotify = true;
                }
                    
              
            }
            finally
            {
                rwLock_ClientInfos.ExitWriteLock();

                if(initNotify)
                {
                    InitScreenInfoNotify(clientId, client);
                }
            } 
        }

        public List<Socket> GetClient(int clientId)
        {
            List<Socket> clients = new List<Socket>();
            rwLock_ClientInfos.EnterReadLock();
            try
            {
                foreach (var item in clientInfos)
                {
                    if (item.Value == clientId)
                    {
                        clients.Add(item.Key);
                    }
                }
            }
            finally
            {

                rwLock_ClientInfos.ExitReadLock();
            }
            return clients;
        }

        public int GetClientId(Socket client)
        {
            int ret = -1;
            rwLock_ClientInfos.EnterWriteLock();
            try
            {
                if (clientInfos.ContainsKey(client))
                    ret = clientInfos[client];
            }
            finally
            {
                rwLock_ClientInfos.ExitWriteLock();
            }
            return ret;
        }

        public void RemoveClient(Socket client)
        {
            rwLock_ClientInfos.EnterWriteLock();
            try
            {
                if (clientInfos.ContainsKey(client))
                {
                    clientInfos.Remove(client);
                }
            }
            finally
            {

                rwLock_ClientInfos.ExitWriteLock();
            }
          
        }

        public void RemoveClient(int clientId)
        {
           rwLock_ClientInfos.EnterWriteLock();
            try
            {
                foreach (var item in clientInfos)
                {
                    if (item.Value == clientId)
                    {
                        clientInfos.Remove(item.Key);
                        break;
                    }
                }
            }
            finally
            {
                rwLock_ClientInfos.ExitWriteLock();
            }
        }

        public void RemoveAllClient()
        {
            rwLock_ClientInfos.EnterWriteLock();
            try
            {
                clientInfos.Clear();
            }
            finally
            {
                rwLock_ClientInfos.ExitWriteLock();
            }
        }

        //DpProcessesContainer的注册
        public void RegisterDataProcess(DataPacketType type, Action<DataPacketType, string, Socket> process)
        {
            rwLock_DpProcesesContainer.EnterWriteLock();
            try
            {
                if (!DpProcessesContainer.ContainsKey(type))
                {
                    DpProcessesContainer.Add(type, process);
                }
            }
            finally
            {
                rwLock_DpProcesesContainer.ExitWriteLock();
            }
        }

        public void RemoveDataProcess(DataPacketType type)
        {
            rwLock_DpProcesesContainer.EnterWriteLock();
            try
            {
                if (DpProcessesContainer.ContainsKey(type))
                {
                    DpProcessesContainer.Remove(type);
                }
            }
            finally
            {
                rwLock_DpProcesesContainer.ExitWriteLock();
            }
          
           
        }

        public Action<DataPacketType, string, Socket> GetProcess(DataPacketType type)
        {
            var ret = default(Action<DataPacketType, string, Socket>);
            rwLock_DpProcesesContainer.EnterReadLock();
            try
            {
                if (DpProcessesContainer.ContainsKey(type))
                {
                    ret = DpProcessesContainer[type];
                }
            }
            finally
            {
                rwLock_DpProcesesContainer.ExitReadLock();
            }
            return ret;
        }


        public void RegisterGroupScreenInfos(int groupId, CurrentGroupScreenInfos infos)
        {
            rwLock_GroupScreenInfos.EnterWriteLock();
            try
            {
                if (!groupScreenInfos.ContainsKey(groupId))
                {
                    groupScreenInfos.Add(groupId, infos);
                }
            }
            finally
            {
                rwLock_GroupScreenInfos.ExitWriteLock();
            }
            
           
        }

        public void RemoveGroupScreenInfos(int groupId)
        {
            rwLock_GroupScreenInfos.EnterWriteLock();
            try
            {
                if (groupScreenInfos.ContainsKey(groupId))
                {
                    groupScreenInfos.Remove(groupId);
                }
            }
            finally
            {
                rwLock_GroupScreenInfos.ExitWriteLock();
            }  
        }

        public void removeAllGroupScreenInfos()
        {
            rwLock_GroupScreenInfos.EnterWriteLock();
            try
            {
                groupScreenInfos.Clear();
            }
            finally
            {
                rwLock_GroupScreenInfos.ExitWriteLock();
            }
        }

        public CurrentGroupScreenInfos GetGroupScreenInfos(int groupId)
        {
            CurrentGroupScreenInfos ret = null;
            rwLock_GroupScreenInfos.EnterReadLock();
            try
            {
                if (groupScreenInfos.ContainsKey(groupId))
                {
                    ret = groupScreenInfos[groupId];
                }
            }
            finally
            {
                rwLock_GroupScreenInfos.ExitReadLock();
            }

            return ret;
        }


        //public void RefreshGroupScreenInfos(int groupId, BlackScreenExtraInfo info)
        //{
        //    CurrentGroupScreenInfos infos = this.GetGroupScreenInfos(groupId);
        //    if (infos == null) return;
        //    DateTime tmpTime = infos.LatestAlarmTime;

        //    ModifyGroupScreenInfos(groupId, tmpTime, info);
        //}

        public void ModifyGroupScreenInfos(int groupId, QueryEventValues eventValues, BlackScreenExtraInfo info)
        {
            CurrentGroupScreenInfos infos_before = this.GetGroupScreenInfos(groupId);
            if (infos_before == null) return;
            var screenStatus_before = infos_before.Screen_Status;
            rwLock_GroupScreenInfos.EnterWriteLock();
            if (groupScreenInfos.ContainsKey(groupId))
            {
                groupScreenInfos[groupId].JudgeScreenStatus(eventValues, info);
            }
            rwLock_GroupScreenInfos.ExitWriteLock();
            CurrentGroupScreenInfos infos_after = this.GetGroupScreenInfos(groupId);
            if (infos_before == null) return;
            var screenStatus_after = infos_after.Screen_Status;

            if (screenStatus_before != screenStatus_after)
            {
                ScreenStatusChanged(groupId);

                //如果是黑屏开始，记录黑屏开始时间
                if (screenStatus_after == ScreenStatus.black_screen)
                {

                    rwLock_GroupScreenInfos.EnterWriteLock();
                    try
                    {
                        if (groupScreenInfos.ContainsKey(groupId))
                        {
                            groupScreenInfos[groupId].blackScreenExtraInfo.start_time = groupScreenInfos[groupId].QueryEventValues.LocalTime.AddSeconds(ProjectTools.getParamInfos().delay_time * 60);
                            Logger.Singleton.Info("blackScreenExtraInfo.start_time:" + groupScreenInfos[groupId].blackScreenExtraInfo.start_time.ToString("yyyy-MM-dd HH:mm:ss"));
                        }
                    }
                    finally
                    {
                        rwLock_GroupScreenInfos.ExitWriteLock();
                    }
                }

                //如果是白屏，记录黑屏结束时间及对应的其它信息
                if (screenStatus_after == ScreenStatus.white_screen)
                {
                    BlackScreenExtraInfo blackScreenExtraInfo = new BlackScreenExtraInfo();
                    bool isSaveData = false;
                    rwLock_GroupScreenInfos.EnterWriteLock();
                    try
                    {
                        if (groupScreenInfos.ContainsKey(groupId))
                        {
                            groupScreenInfos[groupId].blackScreenExtraInfo.end_time = groupScreenInfos[groupId].QueryEventValues.LocalTime;
                            groupScreenInfos[groupId].blackScreenExtraInfo.duration = groupScreenInfos[groupId].blackScreenExtraInfo.end_time - groupScreenInfos[groupId].blackScreenExtraInfo.start_time;
                            groupScreenInfos[groupId].blackScreenExtraInfo.source_sort = info.source_sort;
                            groupScreenInfos[groupId].blackScreenExtraInfo.source = info.source;
                            groupScreenInfos[groupId].blackScreenExtraInfo.Category = info.Category;
                            groupScreenInfos[groupId].blackScreenExtraInfo.area_code = info.area_code;
                            groupScreenInfos[groupId].blackScreenExtraInfo.priority = info.priority;
                            groupScreenInfos[groupId].blackScreenExtraInfo.condition_name = info.condition_name;
                            groupScreenInfos[groupId].blackScreenExtraInfo.block = info.block;
                            groupScreenInfos[groupId].blackScreenExtraInfo.description = info.description;
                            //数据库存储对应的黑屏信息
                            blackScreenExtraInfo = groupScreenInfos[groupId].blackScreenExtraInfo;
                            Logger.Singleton.Info("blackScreenExtraInfo.end_time:" + blackScreenExtraInfo.end_time.ToString("yyyy-MM-dd HH:mm:ss"));
                            isSaveData = true;
                        }
                    }
                    finally
                    {
                        rwLock_GroupScreenInfos.ExitWriteLock();
                    }
                    if (isSaveData)
                        SaveBlackScreenInfo(groupId, blackScreenExtraInfo);
                }
            }
        }

        public void RefreshGroupScreenInfos(int id)
        {
            CurrentGroupScreenInfos infos_before = this.GetGroupScreenInfos(id);
            if (infos_before == null) return;
            var screenStatus_before = infos_before.Screen_Status;
            rwLock_GroupScreenInfos.EnterWriteLock();
            if (groupScreenInfos.ContainsKey(id))
            {
                groupScreenInfos[id].RefreshScreenStatus();
            }
            rwLock_GroupScreenInfos.ExitWriteLock();
            CurrentGroupScreenInfos infos_after = this.GetGroupScreenInfos(id);
            if (infos_before == null) return;
            var screenStatus_after = infos_after.Screen_Status;

            if (screenStatus_before != screenStatus_after)
            {
                ScreenStatusChanged(id);

                //如果是黑屏开始，记录黑屏开始时间
                if (screenStatus_after == ScreenStatus.black_screen)
                {

                    rwLock_GroupScreenInfos.EnterWriteLock();
                    try
                    {
                        if (groupScreenInfos.ContainsKey(id))
                        {
                            groupScreenInfos[id].blackScreenExtraInfo.start_time = groupScreenInfos[id].QueryEventValues.LocalTime.AddSeconds(ProjectTools.getParamInfos().delay_time * 60);
                            Logger.Singleton.Info("blackScreenExtraInfo.start_time:" + groupScreenInfos[id].blackScreenExtraInfo.start_time.ToString("yyyy-MM-dd HH:mm:ss"));
                        }
                    }
                    finally
                    {
                        rwLock_GroupScreenInfos.ExitWriteLock();
                    }
                }

                //如果是白屏，记录黑屏结束时间及对应的其它信息
                if (screenStatus_after == ScreenStatus.white_screen)
                {
                    BlackScreenExtraInfo blackScreenExtraInfo = new BlackScreenExtraInfo();
                    bool isSaveData = false;
                    rwLock_GroupScreenInfos.EnterWriteLock();
                    try
                    {
                        if (groupScreenInfos.ContainsKey(id))
                        {
                            groupScreenInfos[id].blackScreenExtraInfo.end_time = groupScreenInfos[id].QueryEventValues.LocalTime;
                            groupScreenInfos[id].blackScreenExtraInfo.duration = groupScreenInfos[id].blackScreenExtraInfo.end_time - groupScreenInfos[id].blackScreenExtraInfo.start_time;
                            //数据库存储对应的黑屏信息
                            blackScreenExtraInfo = groupScreenInfos[id].blackScreenExtraInfo;
                            Logger.Singleton.Info("blackScreenExtraInfo.end_time:" + blackScreenExtraInfo.end_time.ToString("yyyy-MM-dd HH:mm:ss"));
                            isSaveData = true;
                        }
                    }
                    finally
                    {
                        rwLock_GroupScreenInfos.ExitWriteLock();
                    }
                    if (isSaveData)
                        SaveBlackScreenInfo(id, blackScreenExtraInfo);
                }
            }
        }

        public void RefreshGroupScreenInfos()
        {
            List<int> groupIds = new List<int>();
            rwLock_GroupScreenInfos.EnterReadLock();
            try
            {
                foreach (var item in groupScreenInfos)
                {
                    groupIds.Add(item.Key);
                }
            }
            finally
            {
                rwLock_GroupScreenInfos.ExitReadLock();
            }

            foreach(var id in groupIds)
            {
                CurrentGroupScreenInfos infos_before = this.GetGroupScreenInfos(id);
                if (infos_before == null) return;
                var screenStatus_before = infos_before.Screen_Status;
                rwLock_GroupScreenInfos.EnterWriteLock();
                if (groupScreenInfos.ContainsKey(id))
                {
                    groupScreenInfos[id].RefreshScreenStatus();
                }
                rwLock_GroupScreenInfos.ExitWriteLock();
                CurrentGroupScreenInfos infos_after = this.GetGroupScreenInfos(id);
                if (infos_before == null) return;
                var screenStatus_after = infos_after.Screen_Status;

                if (screenStatus_before != screenStatus_after)
                {
                    ScreenStatusChanged(id);

                    //如果是黑屏开始，记录黑屏开始时间
                    if (screenStatus_after == ScreenStatus.black_screen)
                    {

                        rwLock_GroupScreenInfos.EnterWriteLock();
                        try
                        {
                            if (groupScreenInfos.ContainsKey(id))
                            {
                                groupScreenInfos[id].blackScreenExtraInfo.start_time = groupScreenInfos[id].QueryEventValues.LocalTime.AddSeconds(ProjectTools.getParamInfos().delay_time * 60);
                                Logger.Singleton.Info("blackScreenExtraInfo.start_time:" + groupScreenInfos[id].blackScreenExtraInfo.start_time.ToString("yyyy-MM-dd HH:mm:ss"));
                            }
                        }
                        finally
                        {
                            rwLock_GroupScreenInfos.ExitWriteLock();
                        }
                    }

                    //如果是白屏，记录黑屏结束时间及对应的其它信息
                    if (screenStatus_after == ScreenStatus.white_screen)
                    {
                        BlackScreenExtraInfo blackScreenExtraInfo = new BlackScreenExtraInfo();
                        bool isSaveData = false;
                        rwLock_GroupScreenInfos.EnterWriteLock();
                        try
                        {
                            if (groupScreenInfos.ContainsKey(id))
                            {
                                groupScreenInfos[id].blackScreenExtraInfo.end_time = groupScreenInfos[id].QueryEventValues.LocalTime;
                                groupScreenInfos[id].blackScreenExtraInfo.duration = groupScreenInfos[id].blackScreenExtraInfo.end_time - groupScreenInfos[id].blackScreenExtraInfo.start_time;
                                //数据库存储对应的黑屏信息
                                blackScreenExtraInfo = groupScreenInfos[id].blackScreenExtraInfo;
                                Logger.Singleton.Info("blackScreenExtraInfo.end_time:" + blackScreenExtraInfo.end_time.ToString("yyyy-MM-dd HH:mm:ss"));
                                isSaveData = true;
                            }
                        }
                        finally
                        {
                            rwLock_GroupScreenInfos.ExitWriteLock();
                        }
                        if (isSaveData)
                            SaveBlackScreenInfo(id, blackScreenExtraInfo);
                    }
                }

            }
           
        }

      

   
        public void DataReceive(byte[] buffer, int len, Socket src)
        {
            //buffer转json
            string jsonData = Encoding.UTF8.GetString(buffer, 0, len);
            //json转为DataPacket
            DataPacket dp = new DataPacket();
            dp.JsonToPacket(jsonData);
            rwLock_DpProcesesContainer.EnterReadLock();
            try
            {
                DpProcessesContainer[(DataPacketType)dp.PacketType]?.Invoke((DataPacketType)dp.PacketType, dp.PacketData, src);
            }
            finally
            {
                rwLock_DpProcesesContainer.ExitReadLock();
            }
        }

        public void SendData2Client(int groupId, DataPacketType type, IPacketData data)
        {
            DataPacket dp = new DataPacket();
            dp.PacketData = data.PacketToJson();
            dp.PacketType = (int)type;
            byte[] buffer = Encoding.UTF8.GetBytes(dp.PacketToJson());

           List<Socket> clients = this.GetClient(groupId);
            foreach (Socket client in clients)
            {
                try
                {
                    client.Send(buffer);
                }
                catch (Exception)
                {
                    Logger.Singleton.Error($"发送消息到客户端:{((IPEndPoint)(client.RemoteEndPoint)).Address.ToString()}:{((IPEndPoint)(client.RemoteEndPoint)).Port}----失败!");
                    continue;
                }
               
            }
        }

        public void ScreenStatusChanged(int groupId)
        {
            CurrentGroupScreenInfos infos = this.GetGroupScreenInfos(groupId);
            if (infos != null)
            {
                DataPacket dp = new DataPacket();
               
                if(infos.Screen_Status == ScreenStatus.black_screen)
                {
                    dp.PacketType = (int)DataPacketType.BlackScreenStart;
                    BlackScreenStartData blackScreenStartData = new BlackScreenStartData();
                    blackScreenStartData.BeginTime = infos.QueryEventValues.LocalTime.AddSeconds(ProjectTools.getParamInfos().delay_time * 60).ToString("yyyy-MM-dd HH:mm:ss");
                    dp.PacketData = blackScreenStartData.PacketToJson();
                    Logger.Singleton.Info("blackScreenStartData.BeginTime:" + blackScreenStartData.BeginTime);
                }
                else if(infos.Screen_Status == ScreenStatus.white_screen)
                {
                    dp.PacketType = (int)DataPacketType.BlackScreenEnd;
                    BlackScreenEndData blackScreenEndData = new BlackScreenEndData();
                    blackScreenEndData.EndTime = infos.QueryEventValues.LocalTime.ToString("yyyy-MM-dd HH:mm:ss");
                    dp.PacketData = blackScreenEndData.PacketToJson();
                    Logger.Singleton.Info("blackScreenEndData.EndTime:" + blackScreenEndData.EndTime);
                }
                byte[] buffer = Encoding.UTF8.GetBytes(dp.PacketToJson());

                List<Socket> clients = this.GetClient(groupId);
                foreach (Socket client in clients)
                {
                    string clientIp = ((IPEndPoint)client.RemoteEndPoint).Address.ToString();
                    int clientPort = ((IPEndPoint)client.RemoteEndPoint).Port;

                    string safePacketData = dp.PacketData.Replace("{", "{{").Replace("}", "}}");

                    Logger.Singleton.Info($"send msg to client:{clientIp}---{clientPort}--{safePacketData}--客户端数量:{clients.Count}");
                    //client如果正常，则发送buffer
                  //  client.Send(buffer);

                    try
                    {
                        client.Send(buffer);
                    }
                    catch (Exception)
                    {
                        Logger.Singleton.Error($"发送消息到客户端:{((IPEndPoint)(client.RemoteEndPoint)).Address.ToString()}:{((IPEndPoint)(client.RemoteEndPoint)).Port}----失败!");
                        continue;
                    }


                }
            }
        }

        public void CheckClientConnect()
        {
            List<Socket> clients = new List<Socket>();
            DataPacket dp = new DataPacket();
            //判断客户端连接状态
            rwLock_ClientInfos.EnterWriteLock();
            foreach (var item in clientInfos)
            {
                try
                {
                    dp.PacketType = -1;
                    dp.PacketData = "";
                    item.Key.Send(Encoding.UTF8.GetBytes(dp.PacketToJson()));
                }
                catch (Exception)
                {

                    clients.Add(item.Key);
                }
            }
           
            foreach (var client in clients)
            {
                Logger.Singleton.Info($"client disconnect--{DateTime.Now.ToString("HH:mm:ss")}");
                clientInfos.Remove(client);
            }
            rwLock_ClientInfos.ExitWriteLock();
        }
        public void InitScreenInfoNotify(int groupId, Socket client)
        {
            CurrentGroupScreenInfos infos = this.GetGroupScreenInfos(groupId);
            if (infos != null)
            {
                DataPacket dp = new DataPacket();

                if (infos.Screen_Status == ScreenStatus.black_screen)
                {
                    dp.PacketType = (int)DataPacketType.BlackScreenStart;
                    BlackScreenStartData blackScreenStartData = new BlackScreenStartData();
                    blackScreenStartData.BeginTime = infos.QueryEventValues.LocalTime.AddSeconds(ProjectTools.getParamInfos().delay_time * 60).ToString("yyyy-MM-dd HH:mm:ss");
                    dp.PacketData = blackScreenStartData.PacketToJson();
                }
                else if (infos.Screen_Status == ScreenStatus.white_screen)
                {
                    dp.PacketType = (int)DataPacketType.BlackScreenEnd;
                    BlackScreenEndData blackScreenEndData = new BlackScreenEndData();
                    blackScreenEndData.EndTime = infos.QueryEventValues.LocalTime.ToString("yyyy-MM-dd HH:mm:ss");
                    dp.PacketData = blackScreenEndData.PacketToJson();
                }
                byte[] buffer = Encoding.UTF8.GetBytes(dp.PacketToJson());

                // client.Send(buffer);
                try
                {
                    client.Send(buffer);
                }
                catch (Exception)
                {
                    Logger.Singleton.Error($"发送消息到客户端:{((IPEndPoint)(client.RemoteEndPoint)).Address.ToString()}:{((IPEndPoint)(client.RemoteEndPoint)).Port}----失败!");
                }
            }
        }

        public void SaveBlackScreenInfo(int groupId, BlackScreenExtraInfo info)
        {
            CurrentGroupScreenInfos infos = this.GetGroupScreenInfos(groupId);
            if (infos != null)
            {
                //数据库存储对应的黑屏信息
                SqlHelper sqlLocal = new SqlHelper(ProjectTools.configDbConnString);
                string sql = "insert into timing_report (start_time, end_time, duration, source_sort, source, " +
                    "category, area_code, priority, condition_name, block, description, groupId,station, groupName, location_tag_name,category_name) values ('" + info.start_time.ToString("yyyy-MM-dd HH:mm:ss") +
                    "','" + info.end_time.ToString("yyyy-MM-dd HH:mm:ss") + "','" + ProjectTools.ConvertTimeSpanToStr(info.duration) + "','" +
                    info.source_sort + "','" + info.source + "','" + info.Category + "'," + info.area_code + ",'" + info.priority + "','" +
                    info.condition_name + "','" + info.block + "','" + info.description + "'," + groupId + ",'" + info.station + "','" + info.groupName + "','" + info.location_tag_name + "','"+ info.category_name+ "')" ;
                try
                {
                    sqlLocal.ExecuteNonQuery(sql);
                }
                catch (Exception)
                {
                    Logger.Singleton.Error("timing_report failure");
                }
               
            }
        }
    }
}
